// pages/sharebox/pattern/pattern.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    latelyList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.getlately()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this
    var user_id = wx.getStorageSync('user_id')
    // wx.getStorageSync({
    //   key: 'user_id',
    //   success: function (res) {
    //     user_id = res.data
    //     console.log('res',res)
    //   },
    // });
    if (res.from === 'button') {
      // 来自页面内转发按钮
      that.getcheckShareDevice();
    }
    return {
      title: "分享缴费器",
      // imageUrl: '../../../images/inviteback.png',
      path: 'pages/sharebox/shareUser/shareUser?tab=2' + '&deviceid=' + that.data.id + '&uid=' + user_id
    }
  },

  //共享家人
  myfamily(e) {
    var that = this;
    wx.navigateTo({
      url: '../relation/relation?id=' + that.data.id,
    })
  },

  shareDevList() {
    wx.navigateTo({
      url: '../shareDevList/shareDevList',
    })
  },

  shareFamily(e) {
    var that = this;
    var shareid = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var aimg = e.currentTarget.dataset.aimg;
    var dname = e.currentTarget.dataset.dname;
    wx.navigateTo({
      url: '../shareFamily/shareFamily?id=' + that.data.id + '&shareid=' + shareid + '&name=' + name + '&aimg=' + aimg + '&dname=' + dname,
    })
  },

  getlately() {
    var that = this;
    app.ajax({
      url: "User/Shared/lately",
      data: {
        device_id: that.data.id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            latelyList: res.data.data
          })
        }
      }
    })
  },

  getcheckShareDevice(){
    var that = this;
    app.ajax({
      url: "User/Shared/checkShareDevice",
      data: {
        device_id: that.data.id,
      },
      success: function (res) {
        if (res.data.code == 1000) {

        } else {
          app.showToast(res.data.message);
        }
      }
    })
  }


})
